/**
 * 
 */
package info.niwota.ziplock;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author qiangli
 * 
 */
public class ItemListAdapter extends BaseAdapter {
	public static class ItemInfo {
		public Drawable icon;
		public int iconRes;
		public String title;
		public Intent intent;
		public Object object;

		public Drawable getIcon() {
			return icon;
		}

		public void setIcon(Drawable icon) {
			this.icon = icon;
		}

		public int getIconRes() {
			return iconRes;
		}

		public void setIconRes(int iconRes) {
			this.iconRes = iconRes;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public Intent getIntent() {
			return intent;
		}

		public void setIntent(Intent intent) {
			this.intent = intent;
		}

		public Object getObject() {
			return object;
		}

		public void setObject(Object object) {
			this.object = object;
		}
	}

	private LayoutInflater inflater;

	protected List<ItemInfo> list;

	protected Context context;

	public ItemListAdapter(Context context) {
		this.context = context;
		init();
	}

	private void init() {
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		list = new ArrayList<ItemInfo>();
	}

	public void reload() {
		list.clear();
		loadItems(context, list);
		notifyDataSetChanged();
	}

	public Intent intentForPosition(int position) {
		if (list == null) {
			return null;
		}
		ItemInfo info = list.get(position);
		return info.getIntent();
	}

	public int getCount() {
		return list != null ? list.size() : 0;
	}

	public ItemInfo getItem(int position) {
		if (list == null) {
			return null;
		}
		ItemInfo info = list.get(position);
		return info;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View view;
		if (convertView == null) {
			view = inflater.inflate(R.layout.list_item, parent, false);
		} else {
			view = convertView;
		}
		bindView(view, list.get(position));
		return view;
	}

	private final void bindView(View view, ItemInfo info) {
		TextView text = (TextView) view.findViewById(R.id.line1);

		ImageView icon = (ImageView) view.findViewById(R.id.icon);
		text.setText(info.getTitle());
		int res = info.getIconRes();
		if (res > 0) {
			icon.setImageResource(res);
		} else {
			Drawable d = info.getIcon();
			icon.setImageDrawable(d);
		}
	}

	public void loadItems(Context context, List<ItemInfo> list) {
		//
	}

	public void onClick(DialogInterface dialog, int which) {
	}
}
